/*
    6. Write a C program to convert a given integer to roman number. Go to the editor

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
    Symbol       Value
    I             1
    V             5
    X             10
    L             50
    C             100
    D             500
    M             1000
    Expected Output:
    Original integer: 12
    Roman number of the said integer: XII
*/

#include <stdio.h>
#include <string.h>

int getLenOfInteger(int n){
    int num = 1;
    while((n = n / 10)){
        num++;
    }
    return num;
}

int getBitOfInteger(int n, int idx){
    int i = 0;
    while(i++ < idx){
        n = n / 10;
    }
    return n % 10;
}



char *integer2Roman(int n, char *str){
    
    char *dist[4][10]={
        {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, /*0-9*/
        {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, /*0,10-90*/
        {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, /*0,100-990*/
        {"", "M", "MM", "MMM"} /*0,1000-3000*/
    };
    char *tmp = str;
    int len = getLenOfInteger(n);
    int i, bitNum;
    for(i = len - 1; i >= 0; i--){
        bitNum = getBitOfInteger(n, i);        
        strcpy(tmp, dist[i][bitNum]);
        tmp += strlen(dist[i][bitNum]);
        printf("bit = %d, bitNum = %d, dist = %s, str = %p\n", i, bitNum, dist[i][bitNum], tmp);
    }

    return str;
}

int main(void)
{
    char str[64];
    int n = 102;
    memset(str, 0, sizeof(str));
    printf("Original integer: %d\n", n);
    printf("Roman number of the said integer: %s\n", integer2Roman(n, str));

    return 0;
}